package com.atguigu.tingshu.user.paid.impl;

import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AssertUtil;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.user.paid.PaidRecordStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component(value = SystemConstant.ORDER_ITEM_TYPE_TRACK)//1002
@Slf4j
public class TrackPaidStrategy implements PaidRecordStrategy {
    @Autowired
    private UserPaidTrackMapper userPaidTrackMapper;

    @Autowired
    private TrackInfoFeignClient trackInfoFeignClient;
    @Override
    public void savePaidRecords(UserPaidRecordVo userPaidRecordVo) {
        //根据声音id查询专辑id
        Long tId = userPaidRecordVo.getItemIdList().get(0);
        Result<Long> albumIdResult = trackInfoFeignClient.findAlbumIdByTrackId(tId);
        AssertUtil.resultAssert(albumIdResult, ResultCodeEnum.DATA_ERROR);
        Long albumId = albumIdResult.getData();
        userPaidRecordVo.getItemIdList().forEach(trackId->{
            UserPaidTrack userPaidTrack = new UserPaidTrack();
            userPaidTrack.setUserId(userPaidRecordVo.getUserId());
            userPaidTrack.setOrderNo(userPaidRecordVo.getOrderNo());
            userPaidTrack.setTrackId(trackId);
            userPaidTrack.setAlbumId(albumId);
            userPaidTrackMapper.insert(userPaidTrack);
        });
    }
}
